﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace comix.pubadministration
{
    public partial class sales : System.Web.UI.Page
    {
        private int totalcount;
        private decimal totalsales;
        private decimal totsalowned;
        private decimal totalpublihser;

        protected void Page_Load(object sender, EventArgs e)
        {
            totalcount = 0;
            totalsales = 0;
            totsalowned = 0;
            totalpublihser = 0;
            UpdateGrid();
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateGrid();
        }

        private void UpdateGrid()
        {
            DateTime dtto = DateTime.Now;
            Session["dtto"] = dtto;
            DateTime todaystart = new DateTime(dtto.Year, dtto.Month, dtto.Day, 0, 0, 0);
            switch (DropDownList1.SelectedIndex)
            {
                case 0:
                    Session["dtfrom"] = todaystart;
                    break;
                case 1:
                    Session["dtfrom"] = todaystart.Subtract(new TimeSpan(7, 0, 0, 0));
                    break;
                case 2:
                    if (todaystart.Month > 1)
                        Session["dtfrom"] = new DateTime(todaystart.Year, todaystart.Month - 1, todaystart.Day, 0, 0, 0);
                    else
                        Session["dtfrom"] = new DateTime(todaystart.Year - 1, 12, todaystart.Day, 0, 0, 0);
                    break;
                case 3:
                    Session["dtfrom"] = new DateTime(todaystart.Year - 1, todaystart.Month, todaystart.Day, 0, 0, 0);
                    break;
                case 4:
                    Session["dtfrom"] = new DateTime(todaystart.Year, todaystart.Month, 1, 0, 0, 0);
                    break;
                case 5:
                    Session["dtfrom"] = new DateTime(todaystart.Year, 1, 1, 0, 0, 0);
                    break;
                case 6:
                    Session["dtfrom"] = BDPfrom.SelectedDate;
                    Session["dtto"] = BDPto.SelectedDate;
                    break;
            }
            BDPfrom.Enabled = DropDownList1.SelectedIndex == 6;
            BDPto.Enabled = DropDownList1.SelectedIndex == 6;
            Button1.Enabled = DropDownList1.SelectedIndex == 6;
            BDPfrom.SelectedDate = (DateTime)Session["dtfrom"];
            BDPto.SelectedDate = (DateTime)Session["dtto"];
            SqlDataSource1.Select(new DataSourceSelectArguments());
            GridView1.DataBind();
        }

        protected void Button1_Command(object sender, CommandEventArgs e)
        {
            UpdateGrid();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView drv = (DataRowView)e.Row.DataItem;
                totalcount += (int)drv.Row["cnt"];
                totalsales += (decimal)drv.Row["total_price"];
                totsalowned += (decimal)drv.Row["net_price"];
                totalpublihser += (decimal)drv.Row["pub_price"];
            }
            else
            {
                if (e.Row.RowType == DataControlRowType.Footer)
                {
                    e.Row.Cells[0].Text = "ΣΥΝΟΛΟ";
                    e.Row.Cells[1].Text = totalcount.ToString();
                    e.Row.Cells[2].Text = totalsales.ToString();
                    e.Row.Cells[3].Text = totsalowned.ToString();
                    e.Row.Cells[4].Text = totalpublihser.ToString();
                }
            }
        }
    }
}
